/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gameai.tickettoride.core;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Christopher
 */
public class TrainDeck {
    
    private ArrayList<TrainCard> cards;
    public final int DECK_SIZE = 110;

    public TrainDeck() {
        cards = new ArrayList<TrainCard>();
        
        //Add color cards
        for (int tp=1; tp <= 8; tp++) {
            for (int num=0; num < 12; num++) {
                cards.add(new TrainCard(tp));
            }
        }
        
        //Add wild cards
        for (int w=0; w <= 14; w++) {
            cards.add(new TrainCard(0));
        }
        
        //Shuffle deck
        shuffle();
    }    
    
    public TrainCard draw(){
        
        if (cards.isEmpty()) {
            restack();
        }
        
        return cards.remove(0);
    }
    
    private void restack() {
        cards = new ArrayList<TrainCard>();
        
        //CHECK PLAYER HANDS FOR CARDS
        
        //Add color cards
        for (int tp=1; tp <= 8; tp++) {
            for (int num=0; num < 12; num++) {
                cards.add(new TrainCard(tp));
            }
        }
        
        //Add wild cards
        for (int w=0; w <= 14; w++) {
            cards.add(new TrainCard(0));
        }
        
        //Shuffle deck
        shuffle();
    }
    
    /**
     * Shuffles the deck.
     */
    public void shuffle(){
        int index1, index2;
        Random generator = new Random();
        
        TrainCard temp;
        
        for (int i=0; i < cards.size(); i++) {
            index1 = generator.nextInt(cards.size()-1);
            index2 = generator.nextInt(cards.size()-1);
            
            temp = cards.get(index2);
            cards.set(index2 , cards.get(index1));
            cards.set(index1, temp );
        }
    }
    
    /**
     * Get number of cards left in deck.
     * @return 
     */
    public int getCardsLeft() {
        return cards.size();
    }
    
}
